package cn.pconline.autoclub.task;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 * @author chensy
 */
public class DateUtils {

    public static Date parseDate(String date) {
        try {
            if (date.length() == 10) {
                return new SimpleDateFormat("yyyy-MM-dd").parse(date);
            }
            if (date.length() == 19) {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
            }
        } catch (ParseException ex) {
        }
        return null;
    }

    public static String format(Date date, String fmt) {
        if (date == null) {
            return "";
        }
        DateFormat formatter = new SimpleDateFormat(fmt);
        return formatter.format(date);
    }

    /**
     * 昨天 开始时间
     *
     * @return
     */
    public static java.sql.Timestamp getLastDayStart(Date curDate) {
        Calendar cur = Calendar.getInstance();
        cur.setTime(curDate);
        cur.set(Calendar.HOUR_OF_DAY, 0);
        cur.set(Calendar.MINUTE, 0);
        cur.set(Calendar.SECOND, 0);
        cur.add(Calendar.DAY_OF_YEAR, -1);
        return new java.sql.Timestamp(cur.getTimeInMillis());
    }

    /**
     * 昨天 结束时间
     *
     * @return
     */
    public static java.sql.Timestamp getLastDayEnd(Date curDate) {
        Calendar cur = Calendar.getInstance();
        cur.setTime(curDate);
        cur.set(Calendar.HOUR_OF_DAY, 23);
        cur.set(Calendar.MINUTE, 59);
        cur.set(Calendar.SECOND, 59);
        cur.add(Calendar.DAY_OF_YEAR, -1);
        return new java.sql.Timestamp(cur.getTimeInMillis());
    }

    public static java.sql.Timestamp getLastDay(Date curDate) {
        Calendar cur = Calendar.getInstance();
        cur.setTime(curDate);
        cur.add(Calendar.DAY_OF_YEAR, -1);
        return new java.sql.Timestamp(cur.getTimeInMillis());
    }

    /**
     * 上一个自然周 开始时间
     *
     * @return
     */
    public static java.sql.Timestamp getLastWeekStart(Date curDate) {
        Calendar cur = Calendar.getInstance();
        cur.setTime(curDate);
        cur.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cur.add(Calendar.DAY_OF_YEAR, -7);
        cur.set(Calendar.HOUR_OF_DAY, 0);
        cur.set(Calendar.MINUTE, 0);
        cur.set(Calendar.SECOND, 0);
        return new java.sql.Timestamp(cur.getTimeInMillis());
    }

    /**
     * 上一个自然周 结束时间
     *
     * @return
     */
    public static java.sql.Timestamp getLastWeekEnd(Date curDate) {
        Calendar cur = Calendar.getInstance();
        cur.setTime(curDate);
        cur.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cur.add(Calendar.DAY_OF_YEAR, -1);
        cur.set(Calendar.HOUR_OF_DAY, 23);
        cur.set(Calendar.MINUTE, 59);
        cur.set(Calendar.SECOND, 59);
        return new java.sql.Timestamp(cur.getTimeInMillis());
    }

    /**
     * 取今天是周几
     *
     * @return
     */
    public static int getTodayIndex() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DAY_OF_WEEK) - 1;
    }

    public static java.sql.Timestamp getTimeStamp(Date d) {
        return new java.sql.Timestamp(d.getTime());
    }

    private DateUtils() {
    }

    /**
     * 得到几天前当天时间
     *
     * @param curDate
     * @param dayNum 负数前几天，正数后几天 00:00:00
     * @return
     */
    public static java.sql.Timestamp getSomeDateStart(Date curDate, int dayNum) {
        Calendar cur = Calendar.getInstance();
        cur.setTime(curDate);
        cur.add(Calendar.DAY_OF_YEAR, dayNum);
        cur.set(Calendar.HOUR_OF_DAY, 0);
        cur.set(Calendar.MINUTE, 0);
        cur.set(Calendar.SECOND, 0);
        return new java.sql.Timestamp(cur.getTimeInMillis());
    }

    /**
     * 得到几天前当天时间
     *
     * @param curDate
     * @param dayNum 负数前几天，正数后几天 00:00:00
     * @return
     */
    public static java.sql.Timestamp getSomeDateEnd(Date curDate, int dayNum) {
        Calendar cur = Calendar.getInstance();
        cur.setTime(curDate);
        cur.add(Calendar.DAY_OF_YEAR, dayNum);
        cur.set(Calendar.HOUR_OF_DAY, 23);
        cur.set(Calendar.MINUTE, 59);
        cur.set(Calendar.SECOND, 59);
        return new java.sql.Timestamp(cur.getTimeInMillis());
    }

    /**
     * 取得当前时间
     *
     * @return
     */
    public static Date getNow() {
        return new Date(System.currentTimeMillis());
    }

    /**
     * 取从startYm开始到当前月份结束的月份列表
     *
     * @param startYm
     * @return
     */
    public static List<Integer> getMonthList(int startYm) {
        return getMonthList(startYm, getTodayYmd() / 100);
    }

    public static List<Integer> getMonthList(int startYm, int endYm) {
        List<Integer> ymList = new ArrayList<Integer>();
        for (int ym = startYm; ym <= endYm; ym = getNextYm(ym)) {
            ymList.add(ym);
        }
        return ymList;
    }

    public static int getTodayYmd() {
        SimpleDateFormat ymdSdf = new SimpleDateFormat("yyyyMMdd");
        int todayYmd = Integer.parseInt(ymdSdf.format(new Date()));
        return todayYmd;
    }

    public static int getTodayYmd(Date cur) {
        SimpleDateFormat ymdSdf = new SimpleDateFormat("yyyyMMdd");
        int todayYmd = Integer.parseInt(ymdSdf.format(cur));
        return todayYmd;
    }

    public static int getNextYm(int ym) {
        int y = ym / 100;
        int m = ym % 100;
        if (m + 1 < 13) {
            return y * 100 + (m + 1);
        } else {
            return (y + 1) * 100 + 1;
        }
    }
}
